package com.hose.pages;

import com.hose.commons.FormUtil;
import com.hose.util.HoseUtils;
import com.microsoft.playwright.ElementHandle;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import io.qameta.allure.Step;
import io.qameta.allure.Story;

import java.util.Objects;

@Story("我的 - 我的单据")
public class BillsPage {

    @Step("选择单据模板：{templateName}")
    public static void selectedTemplate(Page page, String templateName) {
        // Click button:has-text("新 建")
        page.locator("button:has-text(\"新 建\")").click();
        HoseUtils.wait(page);
        // Fill [placeholder="搜索"]
        page.locator("[placeholder=\"搜索\"]").fill(templateName);
        // Click button:has-text("搜 索")
        page.locator("button:has-text(\"搜 索\")").click();
        // Click mark:has-text("JK报销单模板001")
        page.locator("mark:has-text(\""+ templateName +"\")").click();
        HoseUtils.wait(page);
    }

    @Step("打开 - 添加费用明细并选择：{feeTypeName}")
    public static void openFeeDialogAndSelected(Page page, String feeTypeName) {
        page.locator("//*[@id='details-container']//*[text()='添加']").click();
        HoseUtils.wait(page);
        FormUtil.selectDownItem(page, "费用类型", feeTypeName);
    }

    @Step("添加费用明细 - 保存")
    public static void saveFee(Page page, String feeType) {
        page.locator("//*[@class='ant-drawer-content' and descendant::*[text()='添加明细']]//*[text()='保 存']").click();
        page.locator("//*[starts-with(@class,'details-wrapper') and descendant::*[contains(text(),'"+feeType+"')]]").waitFor();
    }

    @Step("提交报销单")
    public static void submit(Page page) {
        page.locator("//button[*[text()='提交送审']]").click();
        HoseUtils.wait(page);
        ElementHandle elementHandle = page.querySelector("//*[text()='未核销借款']");
        if (Objects.nonNull(elementHandle)) {
            page.locator("//button[*[text()='继 续']]").click();
        }
        page.locator("//*[@role='dialog' and descendant::*[text()='审批流程']]//button[*[text()='提 交']]").click();
        HoseUtils.wait(page);
        page.locator("//*[text()='关闭']").click();
    }
}
